home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / Quill / Source / PaneEditor.cpp < prev    next >
Text File  |  1997-08-10  |  9KB  |  381 lines

  1. /*
  2.  *  File:       PaneEditor.cpp
  3.  *  Summary:       A view that knows how to edit a TPane.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <->    10/15/96    JDJ        Created
  12.  */
  13.  
  14. #include "PaneEditor.h"
  15.  
  16. #include <ZControl.h>
  17. #include <ZStringUtils.h>
  18. #include <ZTextBox.h>
  19. #include <ZUndoableCommand.h>
  20. #include <ZWindow.h>
  21.  
  22. #include "Document.h"
  23.  
  24.  
  25. // ===================================================================================
  26. //    class CPaneInfo
  27. // ===================================================================================
  28.     
  29. //---------------------------------------------------------------
  30. //
  31. // CPaneInfo::CPaneInfo ()
  32. //
  33. //---------------------------------------------------------------
  34. CPaneInfo::CPaneInfo()
  35. {
  36. }
  37.  
  38.  
  39. //---------------------------------------------------------------
  40. //
  41. // CPaneInfo::CPaneInfo (SPaneInfo, string)
  42. //
  43. //---------------------------------------------------------------
  44. CPaneInfo::CPaneInfo(const SPaneInfo& paneInfo, const string& name) : SPaneInfo(paneInfo)
  45. {
  46.     className = name;
  47. }
  48.  
  49. #pragma mark -
  50.  
  51. // ===================================================================================
  52. //    class CNewEditPaneCommand
  53. // ===================================================================================
  54. class CNewEditPaneCommand : public TUndoableCommand {
  55.  
  56.     typedef TUndoableCommand Inherited;
  57.  
  58. //-----------------------------------
  59. //    Initialization/Destruction
  60. //
  61. public:
  62.     virtual             ~CNewEditPaneCommand();
  63.     
  64.                         CNewEditPaneCommand(TPane* pane, const CPaneInfo& oldInfo, const CPaneInfo& newInfo);
  65.     
  66. //-----------------------------------
  67. //    New API
  68. //
  69. public:
  70.     static    void         UpdatePane(TPane* pane, const CPaneInfo& newInfo);
  71.  
  72. //-----------------------------------
  73. //    Inherited API
  74. //
  75. public:
  76.     virtual string        GetText() const;
  77.  
  78.     virtual bool         IsValid() const;
  79.  
  80. protected:
  81.     virtual void         OnDo();
  82.  
  83.     virtual void         OnUndo();
  84.  
  85.     virtual void         OnRedo();
  86.             
  87. //-----------------------------------
  88. //    Member data
  89. //
  90. protected:
  91.     TSafePtr<TPane>    mPane;
  92.     CPaneInfo        mOldInfo;
  93.     CPaneInfo        mNewInfo;
  94. };
  95.  
  96.  
  97. //---------------------------------------------------------------
  98. //
  99. // CNewEditPaneCommand::~CNewEditPaneCommand
  100. //
  101. //---------------------------------------------------------------
  102. CNewEditPaneCommand::~CNewEditPaneCommand()
  103. {
  104. }
  105.  
  106.  
  107. //---------------------------------------------------------------
  108. //
  109. // CNewEditPaneCommand::CNewEditPaneCommand
  110. //
  111. //---------------------------------------------------------------
  112. CNewEditPaneCommand::CNewEditPaneCommand(TPane* pane, const CPaneInfo& oldInfo, const CPaneInfo& newInfo) : mPane(pane)
  113. {
  114.     ASSERT(pane != nil);
  115.         
  116.     mOldInfo = oldInfo;
  117.     mNewInfo = newInfo;
  118.  
  119.     TWindow* window = dynamic_cast<TWindow*>(mPane->GetTopView());
  120.     if (window != nil)
  121.         mContext = window->GetUndoContext();
  122.     else
  123.         mContext = nil;
  124.     mDelete  = mContext == nil;
  125. }
  126.  
  127.  
  128. //---------------------------------------------------------------
  129. //
  130. // CNewEditPaneCommand::UpdatePane                        [static]
  131. //
  132. //---------------------------------------------------------------
  133. void CNewEditPaneCommand::UpdatePane(TPane* pane, const CPaneInfo& info)
  134. {
  135.     ASSERT(pane != nil);
  136.     
  137.     if (info.visible)
  138.         pane->Show();
  139.     else
  140.         pane->Hide();
  141.     
  142.     pane->SetOpaque(info.opaque);
  143.     pane->SetClassName(info.className);
  144.     pane->SetName(info.name);
  145.     
  146.     pane->SetRefCon(info.refCon);
  147.  
  148.     TWindow* window = dynamic_cast<TWindow*>(pane->GetTopView());
  149.     ASSERT(window != nil);
  150.     
  151.     CDocument* doc = dynamic_cast<CDocument*>(window->GetSuperCommander());
  152.     ASSERT(doc != nil);
  153.     
  154.     CCustomClasses* classes = doc->GetCustomPanes();
  155.     string baseClass = typeid(*pane).name();
  156.     classes->AddClass(info.className, baseClass);
  157. }
  158.  
  159.  
  160. //---------------------------------------------------------------
  161. //
  162. // CNewEditPaneCommand::GetText
  163. //
  164. //---------------------------------------------------------------
  165. string CNewEditPaneCommand::GetText() const
  166. {
  167.     return "";                                // should be inside a transaction
  168. }
  169.  
  170.  
  171. //---------------------------------------------------------------
  172. //
  173. // CNewEditPaneCommand::IsValid
  174. //
  175. //---------------------------------------------------------------
  176. bool CNewEditPaneCommand::IsValid() const
  177. {
  178.     return mPane.TargetExists();
  179. }
  180.  
  181.  
  182. //---------------------------------------------------------------
  183. //
  184. // CNewEditPaneCommand::OnDo
  185. //
  186. //---------------------------------------------------------------
  187. void CNewEditPaneCommand::OnDo()
  188. {
  189.     CNewEditPaneCommand::UpdatePane(mPane.Get(), mNewInfo);
  190. }
  191.  
  192.  
  193. //---------------------------------------------------------------
  194. //
  195. // CNewEditPaneCommand::OnUndo
  196. //
  197. //---------------------------------------------------------------
  198. void CNewEditPaneCommand::OnUndo()
  199. {
  200.     CNewEditPaneCommand::UpdatePane(mPane.Get(), mOldInfo);
  201. }
  202.  
  203.  
  204. //---------------------------------------------------------------
  205. //
  206. // CNewEditPaneCommand::OnRedo
  207. //
  208. //---------------------------------------------------------------
  209. void CNewEditPaneCommand::OnRedo()
  210. {
  211.     CNewEditPaneCommand::UpdatePane(mPane.Get(), mNewInfo);
  212. }
  213.  
  214. #pragma mark -
  215.  
  216. // ===================================================================================
  217. //    CPaneEditor
  218. // ===================================================================================
  219.  
  220. static TReanimatorRegister<CPaneEditor> sNewPaneEditorRegistrar;
  221.  
  222. //---------------------------------------------------------------
  223. //
  224. // CPaneEditor::~CPaneEditor
  225. //
  226. //---------------------------------------------------------------
  227. CPaneEditor::~CPaneEditor()
  228. {
  229. }
  230.  
  231.  
  232. //---------------------------------------------------------------
  233. //
  234. // CPaneEditor::CPaneEditor
  235. //
  236. //---------------------------------------------------------------
  237. CPaneEditor::CPaneEditor(TView* superView) : CRootPaneEditor(superView)
  238. {
  239.     mPane = nil;
  240. }
  241.  
  242.  
  243. //---------------------------------------------------------------
  244. //
  245. // CPaneEditor::SetPane
  246. //
  247. //---------------------------------------------------------------
  248. void CPaneEditor::SetPane(TPane* pane)
  249. {
  250.     ASSERT(pane != nil);
  251.     ASSERT(mPane == nil);
  252.     
  253.     mPane = pane;
  254.     
  255.     mOldInfo = CPaneInfo(mPane->GetInfo(), mPane->GetClassName());
  256.         
  257.     this->SetEditorInfo(mOldInfo);
  258. }
  259.  
  260.  
  261. //---------------------------------------------------------------
  262. //
  263. // CPaneEditor::Create                                    [static]
  264. //
  265. //---------------------------------------------------------------
  266. MReanimatable* CPaneEditor::Create(MReanimatable* parent)
  267. {
  268.     return new CPaneEditor(dynamic_cast<TView*>(parent));
  269. }
  270.  
  271.  
  272. //---------------------------------------------------------------
  273. //
  274. // CPaneEditor::Apply
  275. //
  276. //---------------------------------------------------------------
  277. void CPaneEditor::Apply()
  278. {
  279.     CPaneInfo info = this->GetEditorInfo();
  280.     
  281.     this->SetPaneInfo(info);
  282. }
  283.  
  284.  
  285. //---------------------------------------------------------------
  286. //
  287. // CPaneEditor::Commit
  288. //
  289. //---------------------------------------------------------------
  290. void CPaneEditor::Commit(TMultipleUndoableCommand* command)
  291. {
  292.     CPaneInfo newInfo = this->GetEditorInfo();
  293.  
  294.     command->AddCommand(new CNewEditPaneCommand(mPane, mOldInfo, newInfo));
  295. }
  296.  
  297.  
  298. //---------------------------------------------------------------
  299. //
  300. // CPaneEditor::Revert
  301. //
  302. //---------------------------------------------------------------
  303. void CPaneEditor::Revert()
  304. {
  305.     this->SetEditorInfo(mOldInfo);
  306.     this->SetPaneInfo(mOldInfo);
  307. }
  308.  
  309.  
  310. //---------------------------------------------------------------
  311. //
  312. // CPaneEditor::GetEditorInfo        
  313. //
  314. //---------------------------------------------------------------
  315. CPaneInfo CPaneEditor::GetEditorInfo() const
  316. {
  317.     CPaneInfo info;
  318.     
  319.     TTextBox* textBox = nil;
  320.     TControl* control = nil;
  321.         
  322.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("Class Name"));
  323.     info.className = textBox->GetText();
  324.     
  325.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("Pane Name"));
  326.     info.name = textBox->GetText();
  327.     
  328.     control = dynamic_cast<TControl*>(this->FindSubPane("Visible"));
  329.     info.visible = control->GetValue();
  330.     
  331.     control = dynamic_cast<TControl*>(this->FindSubPane("Opaque"));
  332.     info.opaque = control->GetValue();
  333.     
  334.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("RefCon"));
  335.     info.refCon = textBox->GetValue();
  336.     
  337.     return info;
  338. }
  339.  
  340.  
  341. //---------------------------------------------------------------
  342. //
  343. // CPaneEditor::SetEditorInfo
  344. //
  345. //---------------------------------------------------------------
  346. void CPaneEditor::SetEditorInfo(const CPaneInfo& info)
  347. {
  348.     TTextBox* textBox = nil;
  349.     TControl* control = nil;
  350.     
  351.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("Class Name"));
  352.     textBox->SetText(info.className);
  353.     
  354.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("Pane Name"));
  355.     textBox->SetText(info.name);
  356.     
  357.     control = dynamic_cast<TControl*>(this->FindSubPane("Visible"));
  358.     control->SetValue(info.visible);
  359.     
  360.     control = dynamic_cast<TControl*>(this->FindSubPane("Opaque"));
  361.     control->SetValue(info.opaque);
  362.     
  363.     textBox = dynamic_cast<TTextBox*>(this->FindSubPane("RefCon"));
  364.     textBox->SetValue(info.refCon);
  365. }
  366.  
  367.  
  368. //---------------------------------------------------------------
  369. //
  370. // CPaneEditor::SetPaneInfo
  371. //
  372. //---------------------------------------------------------------
  373. void CPaneEditor::SetPaneInfo(const CPaneInfo& info)
  374. {
  375.     CNewEditPaneCommand::UpdatePane(mPane, info);
  376. }
  377.  
  378.  
  379.  
  380.  
  381.